/*
 * (C) Copyright 2024 赤い月 All Rights Reserved.
 * Description：
 * Author：赤い月
 * Date: 2024/2/11
 * Modify Record:
 */
#include <QGuiApplication>
#include <QQmlApplicationEngine>

#ifdef WIN32
#include "app_dmp.h"
#endif

#include <QtQml/qqmlextensionplugin.h>
#include <QDir>
#include <QGuiApplication>
#include <QLocale>
#include <QLoggingCategory>
#include <QNetworkProxy>
#include <QProcess>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickWindow>
#include <QSslConfiguration>
#include <QTranslator>
#include "AppInfo.h"
#include "Helpers/Log.h"
#include "Helpers/settingshelper.h"
#include "Version.h"

#include "Components/CircularReveal.h"
#include "Components/FileWatcher.h"
#include "Components/FpsItem.h"

int main(int argc, char* argv[]) {
#ifdef WIN32
  ::SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
  qputenv("QT_QPA_PLATFORM", "windows:darkmode=2");
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
  qputenv("QT_QUICK_CONTROLS_STYLE", "Basic");
#else
  qputenv("QT_QUICK_CONTROLS_STYLE", "Default");
#endif
#ifdef Q_OS_LINUX
  // fix bug UOSv20 does not print logs
  qputenv("QT_LOGGING_RULES", "");
  // fix bug UOSv20 v-sync does not work
  qputenv("QSG_RENDER_LOOP", "basic");
#endif
  QGuiApplication::setOrganizationName("SynoMusic");
  QGuiApplication::setOrganizationDomain(
      "https://gitcode.com/weixin_46341175/SynoMusic");
  QGuiApplication::setApplicationName("SynoMusic");
  QGuiApplication::setApplicationDisplayName("SynoMusic");
  QGuiApplication::setApplicationVersion(APPLICATION_VERSION);
  SettingsHelper::getInstance()->init(argv);
  Log::setup("SynoMusic");
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
  QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
#endif
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
  QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
  QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
      Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
#endif

  QGuiApplication app(argc, argv);

  app.setWindowIcon(QIcon(":/SynoMusicPlayer/Resources/Images/Icons/logo.ico"));

  QQmlApplicationEngine engine;

  engine.rootContext()->setContextProperty("AppInfo", AppInfo::getInstance());
  engine.rootContext()->setContextProperty("SettingsHelper",
                                           SettingsHelper::getInstance());

  qmlRegisterType<CircularReveal>("SynoMusicPlayer", 1, 0, "CircularReveal");
  qmlRegisterType<FileWatcher>("SynoMusicPlayer", 1, 0, "FileWatcher");
  qmlRegisterType<FpsItem>("SynoMusicPlayer", 1, 0, "FpsItem");

  const QUrl url(QStringLiteral("qrc:/SynoMusicPlayer/Views/App.qml"));
  QObject::connect(
      &engine, &QQmlApplicationEngine::objectCreated, &app,
      [url](QObject* obj, const QUrl& objUrl) {
        if (!obj && url == objUrl)
          QCoreApplication::exit(-1);
      },
      Qt::QueuedConnection);
  engine.load(url);

  const int exec = QGuiApplication::exec();
  if (exec == 931) {
    QProcess::startDetached(qApp->applicationFilePath(), QStringList());
  }
  return exec;
}
